var config = require('../../script/config');
var http = require('../../script/api.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getOpenId();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  getOpenId() {
    if (app.globalData.openid == null || app.globalData.openid == "") {
      wx.login({
        success(res) {
          if (res.code) {
            http.WxAuthCode(res.code).then(res1 => {
              if (res1.code === 200) {
                app.globalData.openid = res1.data.openId;
                wx.reLaunch({
                  url: '../index/index?code=' + res1.data.openId
                })
              }else{
                wx.showToast({
                  icon: 'none',
                  title: res.info,
                  duration: config.toast.duration
                })
              }
            });
          }else{
            wx.showToast({
              icon: 'none',
              title: '登录失败',
              duration: config.toast.duration
            })
          }
        },
        fail(res){
          wx.showToast({
            icon: 'none',
            title: res.errMsg + res.errno,
            duration: config.toast.duration
          })
        }
      })
    }else{
      wx.reLaunch({
        url: '../index/index?code=' + app.globalData.openid
      })
    }
  },
})